package com.caigou.modules.vote.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.bean.CommentItemBean
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.vote.repository.VoteRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/31
 */
@HiltViewModel
class CommentViewModel @Inject constructor(private val repository: VoteRepository) : ViewModel() {

    val commentDetailLiveData = MutableLiveData<Resource<CommentItemBean>>()
    val replyLiveData = MutableLiveData<Resource<String>>()
    val commentReplyLiveData = MutableLiveData<Resource<String>>()

    fun getCommentDetail(dataId: Int, replyId: Int, type: Int, userId: String) {
        commentDetailLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getCommentDetail(dataId, replyId, type, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<CommentItemBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    commentDetailLiveData.value = it
                }
        }
    }

    fun setReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, dataId: Int, type: Int) {
        replyLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setReply(content, files, senderId, receiverId, dataId, type)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    replyLiveData.value = it
                }
        }
    }

    fun setCommentReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, dataId: Int, parentId: Int, type: Int) {
        commentReplyLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setCommentReply(content, files, senderId, receiverId, dataId, parentId, type)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    commentReplyLiveData.value = it
                }
        }
    }
}